<?php $this->headLink()
->appendStylesheet($this->Css('button.css'))
->appendStylesheet($this->js('plugins/cluetip/jquery.cluetip.css'));
$this->headScript()
->appendFile($this->js('plugins/jquery.form.js'))
->appendFile($this->js('plugins/jquery.hoverIntent.js'))
->appendFile($this->js('plugins/jquery.bgiframe.min.js'))
->appendFile($this->js('plugins/cluetip/jquery.cluetip.js'));
?>
<script type="text/javascript">
$(document).ready(function(){
	// 列出各班排課數統計
	getClassTotal();

	$("#year_semester").change(function(){
		$("#setform").submit();
	});

	$("#sel_grade").change(function(){
		$("#setform").submit();
		});

	$("#sel_course").change(function(){
		var id = 't-'+$(this).val();
		$(".subjectSelected").removeClass('subjectSelected');
		$("."+id).addClass('subjectSelected');
	});

	$(".selTd").click(function(){
		var id = $(this).attr('id');
		var old_id = $(this);
		if ($(this).hasClass('nodata')) {
			var year_semester = $("#year_semester").val();
			var sel_class = $("#sel_grade").val();
			var sel_course = $("#sel_course").val();
			if (sel_course) {
				$.getJSON('<?php echo $this->route('add')?>',{year_semester:year_semester ,sel_class:sel_class , id:id,sel_course:sel_course},function(data){
						old_id.attr('id','td-'+data.time_sn)
						var html = '<span class="ui-icon ui-icon-circle-close"/>'+
						'<span class="course_name">'+data.name+'</span>'+
						'<br/>'+
						'<span class="teacher_name">'+data.teacher_name+'</span>';
						old_id.html(html).removeClass('nodata').addClass('t-'+data.subject_sn+'-'+data.teacher_sn);
						getCourse(); // 重算課程
						getClassTotal(); // 重統計班級排課
					});
			}
		}
		else {
			if (confirm('<?php echo $this->translate('Confirm to delete Timetable  ?')?>')){
				$.getJSON('<?php echo $this->route('delete')?>',{sn:id},function(response){
					$("#"+id).removeClass().empty().html('<span class="ui-icon ui-icon-circle-plus"></span>').attr('id',"td-"+response).addClass('nodata');
					getCourse(); // 重算課程
					getClassTotal(); // 重統計班級排課
					});
			}
		}
	});
	//$('td.selTd').cluetip({splitTitle: '|',arrows: true});



	$(".selTd").mousemove(function(){
		$(this).addClass('selected');
	}).mouseout(function(){
		$(this).removeClass('selected');
		});

	$("#teacherList tr").mousemove(function(){
		$(this).addClass('selected');
	}).mouseout(function(){
		$(this).removeClass('selected');
		}).click(function(){
			$("#teacherList tr").removeClass('subject_seleted');
			$(this).addClass('subject_seleted');

		});

	$("#download-course").click(function(){
		var year_semester = $("#year_semester").val();
		var sel_grade = $("#sel_grade").val();
		window.location = '<?php echo $this->route('downloadodt')?>/year_semester/'+year_semester;
	});

	function getCourse(){
		var year_semester = $("#year_semester").val();
		var sel_grade = $("#sel_grade").val();

		$.get('<?php echo $this->route('getcourse')?>',{year_semester:year_semester , sel_grade:sel_grade},function(data){
			$("#sel_course").html(data);
			$("#sel_course").trigger('change');
		});
	}
	// 列出班級排課統計
	function getClassTotal() {
		$.get('<?php echo $this->route('total-course')?>',{
			  year_semester: $("#year_semester").val()
			},function(data){
			$("#class-total").html(data);
		});

	}

//
//	$("#courseList tbody tr:even").addClass('even');
//	$("#courseList tbody tr:odd").addClass('odd');
//	$("#teacherList tbody tr:even").addClass('even');
//	$("#teacherList tbody tr:odd").addClass('odd');


	getCourse();
});
</script>
<style>
#courseList,#toolList { float:left;}
#toolList {margin:10px}
#courseList table { background:#ccc}
#courseList tr {background:#fff}
#courseList td {padding:5px;text-align:center;width:75px;}
.course_name  {color:navy;}
.teacher_name  {color:blue}
.selTd{cursor:pointer}
.subject_seleted{background:#00f; color: #fff}
.selected {
background:#ff0;
text-decoration:underline;
}
.subjectSelected {background:lime;}
.yetSign{color:red;font-size:large;}
.title-td {white-space: nowrap; background:#def}
#class-total {border:#ccc solid thin ;margin-top:45px; overflow: auto;height: 500px; padding:3px}
</style>

<div id="courseList">
<form id="setform" action="" method="post">
<p>
<select name="year_semester" id="year_semester">
<?php echo  $this->YearSemesterOptions($this->year_semester) ?>
</select>
<select name="sel_grade" id="sel_grade" validate="required:true" >
<option value=""><?php echo $this->translate('Select class')?></option>
<?php echo  $this->classNameOptions($this->year_semester,$this->sel_grade)?>
</select>
<select name="sel_course" id="sel_course"></select>
</p>
<table class="ui-widget-content">
<thead>
<tr>
<th class="title-td"><?php echo  $this->translate('section')?></th>
<th class="title-td"><?php echo  $this->translate('time')?></th>
<?php for($i=0 ;$i < $this->days;$i++):?>
<th><?php echo  $this->tdays[$i+1]?></th>
<?php endfor;?>
</tr>
</thead>
<tbody>
<?php foreach ($this->data as $val):?>
<tr>
<td class="title-td"><?php echo $val['section']?></td>
<td class="title-td"><?php echo $val['time_start']?> ~ <?php echo $val['time_end']?></td>
<?php for($j=1; $j<$this->days+1 ; $j++):?>
<?php
$data = null;
if (isset($this->course_arr[$val['sn']][$j]))
$data = $this->course_arr[$val['sn']][$j];
?>
<?php if ($data):?>
<td id="td-<?php echo $data['time_sn']?>" class="selTd  t-<?php echo $data['subject_sn']?>-<?php echo $data['teacher_sn']?>"   >
<span class="ui-icon ui-icon-circle-close"></span>
<span class="course_name" ><?php echo $data['name']?></span>
<br/>
<span class="teacher_name" ><?php echo $data['teacher_name']?></span>

<?php else:?>
<td id="td-<?php echo $j?>-<?php echo $val['sort']?>" class="selTd  nodata" >
<span class="ui-icon ui-icon-circle-plus"></span>
<?php endif;?>
</td>
<?php endfor;?>
</tr><?php endforeach;?>
</tbody>
</table>
</form>
</div>
<div id="toolList" >
<a id="download-course" class="fg-button ui-state-default fg-button-icon-left ui-corner-all" >
<span class="ui-icon ui-icon-disk" ></span>
<?php echo $this->translate('download course')?>
</a>
<div id="class-total">

</div>

</div>
<div style="clear:both"></div>